package com.taxshare.seller.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.BlankInvoiceWarningEntity;
import com.taxshare.seller.entity.StockDetailEntity;
import com.taxshare.seller.mapper.BlankInvoiceWarningMapper;
import com.taxshare.seller.view.dto.SetUpEarlyWarningDTO;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author YANG
 * @date 2021/3/22
 */
@Service
@Slf4j
public class BlankInvoiceWarningService extends ServiceImpl<BlankInvoiceWarningMapper, BlankInvoiceWarningEntity> {

    @Autowired
    StockDetailService stockDetailService;

    /**
     * 设置发票预警值
     *
     * @param setUpEarlyWarningDTO
     * @return
     */
    public Response setUpEarlyWarning(SetUpEarlyWarningDTO setUpEarlyWarningDTO) {
        BlankInvoiceWarningEntity blankInvoiceWarningEntity = new BlankInvoiceWarningEntity();
        try {
            blankInvoiceWarningEntity.setWarnNumber(Long.parseLong(setUpEarlyWarningDTO.getWarnNumber()));
        } catch (NumberFormatException e) {
            return Response.error("SEL_028");
        }
        blankInvoiceWarningEntity.setUuid(UUIDUitl.UUID32(false));
        blankInvoiceWarningEntity.setLinkId(setUpEarlyWarningDTO.getUuid());
        blankInvoiceWarningEntity.setSellerName(setUpEarlyWarningDTO.getSellerName());
        blankInvoiceWarningEntity.setSellerTaxNo(setUpEarlyWarningDTO.getSellerTaxNo());
        blankInvoiceWarningEntity.setInvoiceType(setUpEarlyWarningDTO.getInvoiceType());
        blankInvoiceWarningEntity.setWarnNumber(Long.parseLong(setUpEarlyWarningDTO.getWarnNumber()));
        blankInvoiceWarningEntity.setOperateUser(UserInfoUtil.getUserNickName());
        blankInvoiceWarningEntity.setOperateTime(DateUtil.now());
        baseMapper.insert(blankInvoiceWarningEntity);
        StockDetailEntity entity = stockDetailService.getById(setUpEarlyWarningDTO.getUuid());
        entity.setWarnNumber(Long.valueOf(setUpEarlyWarningDTO.getWarnNumber()));
        stockDetailService.updateById(entity);
        return Response.success();
    }

    /**
     * 余票预警提示
     *
     * @param setUpEarlyWarningDTO
     * @return
     */
    public Response queryStockWarning(SetUpEarlyWarningDTO setUpEarlyWarningDTO) {

        StockDetailEntity entity = stockDetailService.getById(setUpEarlyWarningDTO.getUuid());
        String invoiceSheet = entity.getInvoiceSheet();
        String warnNumber = entity.getWarnNumber().toString();

        if (Integer.parseInt(warnNumber) > Integer.parseInt(invoiceSheet)) {

            String uuid = UUIDUitl.UUID32(false);
            String messageContent = "空白发票还剩"+invoiceSheet +"张，请尽快补充。";
            baseMapper.insertWarningMessage(uuid,messageContent);
            return Response.success();
        } else {
            return null;
        }

    }


    /**
     * 发票预警设置记录
     *
     * @param setUpEarlyWarningDTO
     * @return
     */
    public Response selectWarningLog(SetUpEarlyWarningDTO setUpEarlyWarningDTO) {
        QueryWrapper<BlankInvoiceWarningEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("link_id", setUpEarlyWarningDTO.getUuid());
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 0,5");
        return Response.success(this.list(queryWrapper));
    }
}
